﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServerPlatform.Web;
using ServerPlatform.Exceptions;
using MapEntities;

namespace WebApp.Controls
{
    public partial class ShapeBindingRuleEditor : BaseDataEditor<ShapeItemBindingRule>
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override void Edit(ShapeItemBindingRule item)
        {
            ctlItemEditor.Edit(item);
            ShapeVisualizationEditor1.Edit(item.Visualization);
            ShapeBehaviourEditor1.Edit(item.Behaviour);
            base.Edit(item);
        }

        public override void ClearCurrent()
        {
            ctlItemEditor.ClearCurrent();
            base.ClearCurrent();                        
        }

        public override ShapeItemBindingRule EndEdit()
        {
            ShapeItemBindingRule itemRule = ctlItemEditor.EndEdit() as ShapeItemBindingRule;
            itemRule.Visualization = ShapeVisualizationEditor1.EndEdit();
            itemRule.Behaviour = ShapeBehaviourEditor1.EndEdit();
            if (string.IsNullOrEmpty(ctlSpatialField.Text.Trim()))
            {
                BusinessException ee = new BusinessException("Spatial Field cannot be empty");
                throw ee;
            }
            else itemRule.SpatialField = ctlSpatialField.Text.Trim();
            try
            {
                if (string.IsNullOrEmpty(ctlRadiusFactor.Text)) itemRule.RadiusFactor = null; else itemRule.RadiusFactor = double.Parse(ctlRadiusFactor.Text.Trim());
            }
            catch
            {
                throw new BusinessException("Radius Factor must be number");
            }
            return itemRule;
        }

        protected override void ShowEntity(ShapeItemBindingRule entity)
        {
            ctlSpatialField.Text = entity.SpatialField;
            ctlRadiusFactor.Text = (!entity.RadiusFactor.HasValue) ? string.Empty : entity.RadiusFactor.ToString();

        }

        public override ShapeItemBindingRule GetEntity()
        {
            return Current;
        }


    }
}